﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using l1cs.server.utils;
using l1cs.server.datatables;
using l1cs.server.templates;

namespace l1cs.server.serverpackets
{
    public class S_ApplyAuction : ServerBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( S_ApplyAuction ).FullName );
 
        private byte[] _byte = null;

        public S_ApplyAuction( int objectId, String houseNumber )
        {
            buildPacket( objectId, houseNumber );
        }

        private void buildPacket( int objectId, String houseNumber )
        {
            int number = Convert.ToInt32( houseNumber );
            L1AuctionBoard autionBoard = AuctionBoardTable.getInstance().getAuctionBoardTable( number );
            int nowPrice =  autionBoard.getPrice() ;
            int bidderId = autionBoard.getBidderId();
            writeC( Opcodes.S_OPCODE_INPUTAMOUNT );
            writeD( objectId );
            writeD( 0 ); // ?
            if ( bidderId == 0 )
            { // 入札者なし
                writeD( nowPrice ); // スピンコントロールの初期価格
                writeD( nowPrice ); // 価格の下限
            }
            else
            { // 入札者あり
                writeD( nowPrice + 1 ); // スピンコントロールの初期価格
                writeD( nowPrice + 1 ); // 価格の下限
            }
            writeD( 2000000000 ); // 価格の上限
            writeH( 0 ); // ?
            writeS( "agapply" );
            writeS( "agapply " + houseNumber );
        }

        override
        public byte[] getContent()
        {
            if ( _byte == null )
            {
                _byte = getBytes();
            }
            return _byte;
        }

        override
        public String getType()
        {
            return "[S] S_ApplyAuction";
        }
    }
}
